package com.xiong.jdbc;

import com.alibaba.fastjson.JSONObject;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class TestDemo {

    @Test
    public void Test2(){
        C3p0DataSource dataSource=new C3p0DataSource();
        Connection connection=dataSource.getConnection();
        JdbcEngine jdbcEngine=new JdbcEngine(connection);

        Map<String,Object> map=new HashMap<>();
        map.put("id",1);
        map.put("note","haha");

        jdbcEngine.insert("m_notice",map);
    }

    @Test
    public void Test1(){
        C3p0DataSource dataSource=new C3p0DataSource();
        Connection connection=dataSource.getConnection();
        JdbcEngine jdbcEngine=new JdbcEngine(connection);
        List<Map<String,Object>> mapList=jdbcEngine.selectMaps("select * from m_note limit ?,?",new Object[]{10,10});
        mapList.forEach(e->{
            System.out.println(JSONObject.toJSONString(e));
        });
    }

    public static void main(String[] args) throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/spring-demo?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        //当数据库连接池中连接不够时，一次性向服务器申请的连接数
        dataSource.setAcquireIncrement(5);
        //初始化时的连接数量
        dataSource.setInitialPoolSize(10);
        // 每个连接中最多可以使用的Statement的个数
        dataSource.setMaxStatementsPerConnection(2);
        //数据库连接池中维护的最多的Statement的个数
        dataSource.setMaxStatements(50);
        dataSource.setMaxPoolSize(20);
        dataSource.setMinPoolSize(5);



        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM m_note");
            Map<String, Object> resultMap = new LinkedHashMap<>(); // 使用LinkedHashMap保证顺序
            while (resultSet.next()) {
                // 处理查询结果
                int columnCount = resultSet.getMetaData().getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultSet.getMetaData().getColumnName(i);
                    Object columnValue = resultSet.getObject(i);
                    resultMap.put(columnName, columnValue);
                }
            }
            System.out.println(JSONObject.toJSONString(resultMap));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                if (statement != null) statement.close();
                if (connection != null) connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 关闭连接池
        dataSource.close();
    }
}
